package org.tale.mscp.sms.aliyun.services;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.tale.mscp.sms.aliyun.entity.SmsLog;
import org.tale.mscp.sms.aliyun.entity.SmsQueryEntity;
import org.tale.mscp.sms.aliyun.entity.SmsSendBatchEntity;
import org.tale.mscp.sms.aliyun.entity.SmsSendEntity;


@Service
public class SmsService {

    public static MethodType ALI_SMS_METHOD = MethodType.POST;

    private static ApplicationContext applicationContext = null;

    @Value("${org.tale.mscp.sms.aliyun.smsdomain}")
    private String aliSmsDomain;

    @Value("${org.tale.mscp.sms.aliyun.smsversion}")
    private String aliSmsVersion;

    @Autowired
    private SmsLogThread smsLogThread;


    public String SendSms(SmsSendEntity smsSendEntity) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("default", smsSendEntity.getAccessKeyId(), smsSendEntity.getSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest aliRequest = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        aliRequest.setSysMethod(ALI_SMS_METHOD);
        aliRequest.setSysDomain(aliSmsDomain);
        aliRequest.setSysVersion(aliSmsVersion);
//        aliRequest.setSysAction("SendSms");
        aliRequest.putQueryParameter("PhoneNumbers", smsSendEntity.getPhoneNumbers());
        aliRequest.putQueryParameter("SignName", smsSendEntity.getSignName());
        aliRequest.putQueryParameter("AccessKeyId", smsSendEntity.getAccessKeyId());
        aliRequest.putQueryParameter("TemplateCode", smsSendEntity.getTemplateCode());
        aliRequest.putQueryParameter("TemplateParam", smsSendEntity.getTemplateParam());
        aliRequest.putQueryParameter("Action", "SendSms");
        CommonResponse response = client.getCommonResponse(aliRequest);
        smsLogThread.SendSmsLogAysnc(SmsLog.TYPE_SEND, "单条发送", "response.getData()");
//        new SaveLogThread(SmsLog.TYPE_SEND,"单条发送",response.getData()).start();
        return response.getData();
    }

    public String SendBatchSms(SmsSendBatchEntity smsSendBatchEntity) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("default", smsSendBatchEntity.getAccessKeyId(), smsSendBatchEntity.getSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest aliRequest = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        aliRequest.setSysMethod(ALI_SMS_METHOD);
        aliRequest.setSysDomain(aliSmsDomain);
        aliRequest.setSysVersion(aliSmsVersion);
//        aliRequest.setSysAction("SendSms");
        aliRequest.putQueryParameter("PhoneNumberJson", smsSendBatchEntity.getPhoneNumberJson());
        aliRequest.putQueryParameter("SignNameJson", smsSendBatchEntity.getSignNameJson());
        aliRequest.putQueryParameter("AccessKeyId", smsSendBatchEntity.getAccessKeyId());
        aliRequest.putQueryParameter("TemplateCode", smsSendBatchEntity.getTemplateCode());
        aliRequest.putQueryParameter("TemplateParamJson", smsSendBatchEntity.getTemplateParamJson());
        aliRequest.putQueryParameter("Action", "SendBatchSms");
        CommonResponse response = client.getCommonResponse(aliRequest);
        return response.getData();
    }

    public String QuerySendDetails(SmsQueryEntity smsQueryEntity) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("default", smsQueryEntity.getAccessKeyId(), smsQueryEntity.getSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest aliRequest = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        aliRequest.setSysMethod(ALI_SMS_METHOD);
        aliRequest.setSysDomain(aliSmsDomain);
        aliRequest.setSysVersion(aliSmsVersion);
        aliRequest.putQueryParameter("PhoneNumber", smsQueryEntity.getPhoneNumber());
        aliRequest.putQueryParameter("SendDate", smsQueryEntity.getSendDate());
        aliRequest.putQueryParameter("PageSize", smsQueryEntity.getPageSize().toString());
        aliRequest.putQueryParameter("CurrentPage", smsQueryEntity.getCurrentPage().toString());
        aliRequest.putQueryParameter("Action", "QuerySendDetails");
        CommonResponse response = client.getCommonResponse(aliRequest);
        return response.getData();
    }


}
